package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.MessageResponse;
import com.innovation.chengzhangriji.http.NewMessageResponse;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.innovation.chengzhangriji.ui.widget.MessageAdapter;
import com.orhanobut.logger.Logger;

public class MessagesActivity extends BaseActivity implements CuteToolbar.OnClickCuteToolbarListener {

    private MessageAdapter mAdapter;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            update();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupLayout();
        update();
    }

    private void setupLayout() {
        setContentView(R.layout.activity_messages);
        CuteToolbar toolbar = (CuteToolbar) findViewById(R.id.toolbar);
        toolbar.setOnClickCuteToolbarListener(this);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new MessageAdapter(this);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

    }

    private void update() {
        LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return;
        }
        final String phone = loginInfo.getPhone();
        final String token = loginInfo.getToken().getAccessToken();
        final String userID = loginInfo.getUserID();
        final int babyID = loginInfo.getBabyID();
        final long serverTS = DatabaseManager.getInstance(this).queryMessageServerTS(userID);
        mAdapter.setServerTS(serverTS);
        Client.requestNewMessage(this, phone, token, babyID, 0, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                NewMessageResponse response = get(value.toString(), NewMessageResponse.class);
                if (response == null) {
                    return;
                }
                long newServerTS = response.getServerTS();
                MessageResponse[] messages = response.getMessages();
                DatabaseManager.getInstance(MessagesActivity.this).updateMessageServerTS(userID, newServerTS);
                mAdapter.set(messages);
                if (newServerTS > serverTS) {
                    Logger.d("有新消息, newServerTs:" + response.getServerTS()+", oldServerTs:"+ serverTS);
                    Config.startGetAllBabyInfoService(MessagesActivity.this);
                }
            }

            @Override
            public void onInnovationFinish() {
                dismissProgressDialog();
            }

            @Override
            public void onInnovationExceptionFinish() {
                dismissProgressDialog();
            }

        }, networkUnavailableSampleImpl());
    }
}
